package at.fhj.itm;

/**
 * Simple Class implementing a queue.
 * 
 * @author Michael Joergler
 * @version 1.0 2012/06/14
 */
public class Queue {

	/**	Maximum size of the queue */
	private int maxSize;
	/** Array implementing the queue */
	private long[] queArray;
	/** Pointer for the first element of the queue */
	private int front;
	/** Pointer for the last element of the queue */
	private int rear;
	/** Counter for the amount of items in the queue */
	private int nItems;

	/**
	 * Default constructor for the Queue class
	 * 
	 * @param 		The size of the Queue
	 */
	public Queue(int s) {
		maxSize = s;
		queArray = new long[maxSize];
		front = 0;
		rear = -1;
		nItems = 0;
	}

	/**
	 * Inserts an element into the queue.
	 * @param j		Element which will be inserted.
	 */
	public void insert(long j) {
		if (rear == maxSize - 1) // deal with wraparound
			rear = -1;
		queArray[++rear] = j; // increment rear and insert
		nItems++;
	}

	/**
	 * Removes an element from the front of the queue.
	 * @return 		The element from the front of the queue which will be removed from the queue.
	 */
	public long remove() {
		long temp = queArray[front++]; // get value and incr front
		if (front == maxSize) // deal with wraparound
			front = 0;
		nItems--; // one less item
		return temp;
	}

	/**
	 * Gets the first element of the queue without removing it.
	 * @return		First element of the queue.
	 */
	public long peekFront() {
		return queArray[front];
	}
	
	/**
	 * Checks if the queue is empty.
	 * @return		The state of the queue. True-->no elements in queue, False-->elements in queue
	 */
	public boolean isEmpty() {
		return (nItems == 0);
	}

	/**
	 * Checks if the queue is full.
	 * @return		The state of the queue. False-->no elements in queue, True-->elements in queue
	 */
	public boolean isFull() {
		return (nItems == maxSize);
	}

	/**
	 * Gets the amount of elements in the queue.
	 * @return		Amount of elements in the queue.
	 */
	public int size() {
		return nItems;
	}
}
